import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';

import 'action.dart';
import 'state.dart';
import 'package:rss_readneed/public.dart';
import 'package:flutter/services.dart';

import 'package:rss_readneed/sn_support/SNAccount.dart';

Widget buildView(punch_cardState state, Dispatch dispatch, ViewService viewService) {

  return GestureDetector(
    onTap: () { FocusScope.of(viewService.context).requestFocus(FocusNode()); },//点击空白收起键盘
    child: Scaffold(
        appBar: AppbarCommon(
          ctx: viewService.context,
          titleString: "工作记忆",
          noBackLeading:IconButton(icon: Icon(Icons.transform), onPressed: (){ dispatch(punch_cardActionCreator.changeFirstPage()); /*切换主页面*/}),
          actions: <Widget>[
          Container(child: SNAccount.instance.isFatch ?
              Container()
              : IconButton(icon: Icon(Icons.link), onPressed: (){ AppNavigator.pushRoute(viewService.context, "sn"); })//跳转sn登录
          )
        ],),
        body: ListView(
          padding: EdgeInsets.only(left: 4,right: 4),
          controller: state.scrollController,
          children: <Widget>[
            Column(
              children: <Widget>[
                Container(
                  child: Column(
                    children: state.model.todayThings.map((string){
                      //over|
                      String newTitleString = string;
                      if (string.contains("over|")) {
                        newTitleString = string.substring(5);
                      }
                      return Container(
                        height: 36,
                        child: ListTile(
                          title: Text(newTitleString),
                          trailing: Checkbox(
                              value: string.contains("over|"),
                              onChanged: (bool isSure){
                                //是否完成 - 选择
                                dispatch(punch_cardActionCreator.doTodayThing(string));
                              }),
                        ),
                      );
                    }).toList(),
                  ),
                ),//今日执行
                SizedBox(height: 10,),
                Container(
                  child: Card(
                    child: Stack(
                      children: <Widget>[
                        //日期选择
                        Positioned(
                            top: 10,
                            left: 10,
//                            width: 110,
//                            height: 25,
                            child:InkWell(
                              child: Container(
                                child: Text(state.model.dateTime,
                                  style: TextStyle(color: Colors.white,fontSize: 16,fontWeight: FontWeight.w900),
                                ),
                                decoration: BoxDecoration(
                                    gradient:LinearGradient(
                                      colors: [Theme.of(viewService.context).primaryColorDark,Theme.of(viewService.context).primaryColorLight],
                                      begin: Alignment.topLeft,
                                      end: Alignment.bottomCenter,
                                    ),
                                    boxShadow: [BoxShadow(color: mainColor,offset: Offset(0, 0),spreadRadius: 2,blurRadius: 0.5)],
                                    borderRadius: BorderRadius.all(Radius.circular(4))
                                ),
                                padding: EdgeInsets.only(top: 3,bottom: 3,left: 8,right: 8),
                              ),
                              onTap: (){ dispatch(punch_cardActionCreator.showDatePickerAction()); },
                            ),
                        ),
                        //时间选择
                        Positioned(
                            child:Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: <Widget>[
                                InkWell(
                                  child: Container(
                                    child: Text(state.model.finishTime.contains(state.model.dateTime) ? state.model.finishTime.substring(state.model.dateTime.length) : state.model.finishTime,
                                      style: TextStyle(fontSize:state.model.finishTime.contains(state.model.dateTime) ? 32 : 24,fontWeight: FontWeight.w600),
                                    ),
                                    margin: EdgeInsets.only(top: 30,bottom: 20),
                                  ),
                                  onTap: (){dispatch(punch_cardActionCreator.showTimePickerAction());},
                                ),
                                Container(
                                  child: state.isToday ? IconButton(icon: Icon(Icons.refresh,color: Colors.blue,), onPressed: (){ dispatch(punch_cardActionCreator.retimeTodayAction());}) : Container(),
                                  margin: EdgeInsets.only(top: 30,bottom: 20),
                                )
                              ],
                            ),
                        ),
                        Positioned(
                          right: 8,
                          bottom: 3,
                          child: InkWell(
                            child: Container(
                              child: RichText(
                                text: TextSpan(
                                    text: "月均下班 ",
                                    style:TextStyle(color: Colors.black38,fontSize: 12,fontWeight: FontWeight.w600),
                                    children: [TextSpan(
                                      text: state.alreadyfinishTime,
                                      style: TextStyle(color: Colors.orange,fontSize: 20,fontWeight: FontWeight.w300),
                                    )]
                                ),
                              ),
                            ),
                            onTap: (){ dispatch(punch_cardActionCreator.pushListVC());/*跳转当月数据列表*/ },
                          )
                        ),
                      ],
                    ),
                  ),
                ),//当前下班时间
                Container(
                  child: TextField(
                    controller: state.controller,
//            autofocus: true,
//            focusNode: state.focusNode,
                    maxLength: 300,
                    maxLines: 10,
                    decoration: InputDecoration(
                      hintText: " 随笔记录",
                      hintStyle: TextStyle(fontSize: 14),
                      border: OutlineInputBorder(),//全边框样式
                    ),
                  ),
                  height: 140,
                  padding: EdgeInsets.all(4),
                ),//随笔记录,
                Container(//当没有内容且不是当天的时候就没有待办的展示
                  child:(state.tomorrowControllers.length == 0 && state.isToday == false) ? Container() : ListTile(
                    leading: Text("待办Biu",//原名称：明日之星
                      style: TextStyle(color: Colors.lightBlue,fontSize: 20,fontWeight: FontWeight.w700),
                    ),),
                ),
                Container(
                    child: Card(
                      child: Column(
                        children: TomorrowThingsItems(state.tomorrowControllers,state.isToday,dispatch),
                      ),
                    )
                ),//明日之星
                Container(
                  child: Row(
                    children: <Widget>[
                      FlatButton(
                          onPressed: null,
                          child: Row(
                            children: <Widget>[
                              Checkbox(
                                  value: state.model.inCount,
                                  onChanged: (bool inCount){
                                    //是否统计
                                    dispatch(punch_cardActionCreator.changeInCountAction(inCount));
                                  }),
                              Text("统计下班")
                            ],
                          )
                      ),
                      SizedBox(width: 10,),
                      Expanded(
                          child: RaisedButton(
                            color: Theme.of(viewService.context).primaryColor,
                            onPressed: (){ dispatch(punch_cardActionCreator.punchCardAction()); },
                            child: Text(state.isChange ? "更新下班" : "下班",
                              style: TextStyle(color: Colors.white,fontSize: 16,fontWeight: FontWeight.w700),
                            ),
                          )
                      )
                    ],
                  ),
                  padding: EdgeInsets.all(4),
                ),//下班
                Container(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: judgeSettingAndList(state.isToday, dispatch),
                  ),
                  padding: EdgeInsets.all(4),
                ),//月度规划,传送门
              ],
            ),
          ],
        )
    ),
  );
}

List<Widget> TomorrowThingsItems(List<TextEditingController> controllers,bool isToday,Dispatch dispatch) {

  List<Widget> list = [];

  for (int i = 0;i < controllers.length;i++) {

    Widget widget = Row(
      children: <Widget>[
        SizedBox(width: 10,),
        Expanded(child: TextField(
          enabled: isToday,//非当天的不能编辑,控制待办
          controller: controllers[i],
//          autofocus: true,
//          focusNode: focusNode,
//        maxLength: 50,//此限制输入长度方式会在右下角有长度的提示
          inputFormatters: [LengthLimitingTextInputFormatter(50)],//限制输入长度50
          decoration: InputDecoration(
            hintText: " 明天要做点什么...",
            hintStyle: TextStyle(fontSize: 12),
          ),
        )),
        Container(
          //只有当天的最后一项才有添加按钮
          child: (i == controllers.length - 1 && i != 4 && isToday == true) ? IconButton(icon: Icon(Icons.add), onPressed:(){ dispatch(punch_cardActionCreator.addTomorrowThingsAction()); }) : IconButton(icon: Icon(Icons.remove), onPressed:(){ dispatch(punch_cardActionCreator.reduceTomorrowThingsAction(i)); }),
          width: 50,
          height: 50,
        )
      ],
    );
    list.add(widget);
  }
  if (list.length == 0) {

    list.add(Container());
  }

  return list;
}

List<Widget> judgeSettingAndList(bool isToday,Dispatch dispatch) {

  List<Widget> list = [];

  //只有当天才开放月度规划
  if (isToday == true) {

    Widget setting = RaisedButton(
      onPressed: (){ dispatch(punch_cardActionCreator.pushSettingVC()); },
      child: Text("月度规划",style: TextStyle(fontWeight: FontWeight.w300,fontSize: 14),),
    );
    list.add(setting);
  }
  list.add(SizedBox(width: 10,));

  //历史查询,暂隐藏
//  RaisedButton(
//    onPressed: (){ dispatch(punch_cardActionCreator.pushListVC()); },
//    child: Text("传送门"),
//  );
  return list;
}
